package com.lzy.system.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzy.exception.BusException;
import com.lzy.system.organization.entity.SysRole;
import com.lzy.system.organization.mapper.SysRoleMapper;
import com.lzy.system.organization.service.ISysRoleService;
import com.lzy.utils.BeanMapHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcc
 * @since 2022-08-13
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    private SysRoleMapper sysRoleMapper;
    @Autowired
    public SysRoleServiceImpl(SysRoleMapper sysRoleMapper) {
        this.sysRoleMapper = sysRoleMapper;
    }

    @Override
    public List<SysRole> selectByUserName(String username) throws BusException {
        return sysRoleMapper.selectByUid(username);
    }

    @Override
    public List<SysRole> getList() throws BusException {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("id");
        List<SysRole> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<SysRole> getListDir() throws BusException {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper();
        queryWrapper.eq("type",1);
        queryWrapper.orderByAsc("id");
        List<SysRole> list = this.list(queryWrapper);
        list.add(new SysRole(){{
            setId(0L);
            setPid(-1L);
            setRole_name("根目录");
        }});
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(SysRole sysRole) throws BusException {
        if (sysRole.getPid() == null) {
            sysRole.setPid(0L);
        }
        return this.save(sysRole);
    }

    @Override
    public boolean update(SysRole sysRole) throws BusException {
        SysRole SysRole = (SysRole) BeanMapHelper.Bean2Bean(sysRole, SysRole.class);
        return this.updateById(SysRole);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delById(long id) throws BusException {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", id);
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusException("当前资源有下级资源不能删除");
        }
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delBatch(List<SysRole> list) throws BusException {
        List<SysRole> links = list.stream().filter(item -> item.getType() == 2).collect(Collectors.toList());
        List<SysRole> dics = list.stream().filter(item -> item.getType() == 1).collect(Collectors.toList());

        List<Long> link_ids = new ArrayList<>();
        List<Long> dic_ids = new ArrayList<>();
        for (SysRole link : links) {
            link_ids.add(link.getId());
        }

        this.removeBatchByIds(link_ids);

        //查询对于的目录下是否还有链接
        for (SysRole dic : dics) {
            QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid",dic.getId());
            long count = this.count(queryWrapper);
            if(count == 0){
                dic_ids.add(dic.getId());
            }
        }
        this.removeBatchByIds(dic_ids);
        return true;
    }

    @Override
    public List<SysRole> getByMidSysRoleList(Long mid) throws BusException {
        return sysRoleMapper.getByMidSysRoleList(mid);
    }
}
